package com.jsjosem.aftersales.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jsjosem.aftersales.R;
import com.jsjosem.aftersales.data.response.NotificationsResponse;

import java.util.List;
import java.util.Objects;

public class ServiceNotifyAdapter extends RecyclerView.Adapter<ServiceNotifyAdapter.ServiceNotifyViewHolder> {

    private final Context mContext;

    private final List<NotificationsResponse> mServiceNotifies;

    private OnItemClickListener mOnItemClickListener;

    private final static String TITLE = "新事件通知";

    private View view;


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public ServiceNotifyAdapter(Context mContext, List<NotificationsResponse> mServiceNotifies) {
        this.mContext = mContext;
        this.mServiceNotifies = mServiceNotifies;
    }

    @NonNull
    @Override
    public ServiceNotifyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.notify_service_item_list, parent, false);
        ServiceNotifyViewHolder serviceNotifyViewHolder = new ServiceNotifyViewHolder(view);
        view.setOnClickListener(v -> {
            if (Objects.nonNull(mOnItemClickListener)) {
                mOnItemClickListener.onItemClick(v, serviceNotifyViewHolder.getAdapterPosition());
            }
        });
        return serviceNotifyViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ServiceNotifyViewHolder holder, int position) {
        NotificationsResponse eventResponse = mServiceNotifies.get(position);
        if (eventResponse == null) {
            return;
        }
        holder.tvServiceNotifyTime.setText(eventResponse.getmCreateTimeFormat());
        holder.tvServiceNotifyTitle.setText(TITLE);
        // 已读，隐藏红点控件
        if (eventResponse.getmStatus() == 1) {
            holder.ivUnread.setVisibility(View.GONE);
        }
        holder.tvCustomerNameValue.setText(eventResponse.getmCustomerName());
        holder.tvEventCodeValue.setText(eventResponse.getmEventNumber());
        holder.tvCrateTimeValue.setText(eventResponse.getmCreateTimeFormat());
        holder.tvCrateByValue.setText(eventResponse.getmCreatorName());
    }

    @Override
    public int getItemCount() {
        return mServiceNotifies.size();
    }

    static class ServiceNotifyViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvServiceNotifyTime;

        private final TextView tvServiceNotifyTitle;

        private final ImageView ivUnread;

        private final TextView tvEventCodeValue;

        private final TextView tvCustomerNameValue;

        private final TextView tvCrateTimeValue;

        private final TextView tvCrateByValue;

        public ServiceNotifyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvServiceNotifyTime = itemView.findViewById(R.id.tv_service_notify_time);
            tvServiceNotifyTitle = itemView.findViewById(R.id.tv_service_notify_title);
            ivUnread = itemView.findViewById(R.id.iv_unread);
            tvEventCodeValue = itemView.findViewById(R.id.tv_event_code_value);
            tvCustomerNameValue = itemView.findViewById(R.id.tv_customer_name_value);
            tvCrateTimeValue = itemView.findViewById(R.id.tv_create_time_value);
            tvCrateByValue = itemView.findViewById(R.id.tv_create_by_value);
        }
    }
}
